﻿using System;
using System.Collections.Generic;
using System.Text;
using ltModel;
using Telerik.Web.UI;

public partial class ltMan_users_userPermission : AdminBasePage
{
    ltModel.BLL.userTeam ubl = new ltModel.BLL.userTeam();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            new RadTreeViewBinder(false).LoadAllNodes(RadTreeView1);
            RadTreeView1.ExpandAllNodes();
            RadTreeView2.DataSource = SqlHelper.Instance.ExecuteReader("select * from lt_article_class");
            RadTreeView2.DataBind();
            RadTreeView2.ExpandAllNodes();

            string id = Request.QueryString["id"];
            if (Str.IsInt(id))
            {
                ltModel.Model.userTeam ut = ubl.GetModel(int.Parse(id));
                CheckBoxList1.Items[0].Selected = ut.HasSh;
                CheckBoxList1.Items[1].Selected = ut.HasTj;
                CheckBoxList1.Items[2].Selected = ut.HasAdd;
                CheckBoxList1.Items[3].Selected = ut.HasDel;
                SetChecked(RadTreeView1, ut.funcqx);
                SetChecked(RadTreeView2, ut.lmqx);
            }
        }
    }
    void SetChecked(RadTreeView treeview, string valueList)
    {
        if (!string.IsNullOrEmpty(valueList))
        {
            string[] values = valueList.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            if (values != null && values.Length > 0)
            {
                foreach (string value in values)
                {
                    RadTreeNode node = treeview.FindNodeByValue(value);
                    if (node != null)
                        node.Checked = true;
                }
            }
        }
    }
    string GetSelected(RadTreeView treeview)
    {
        IList<RadTreeNode> nodes = treeview.CheckedNodes;
        if (nodes != null && nodes.Count > 0)
        {
            StringBuilder builder = new StringBuilder();
            int max = nodes.Count - 1;
            for (int i = 0; i <= max; i++)
            {
                builder.Append(nodes[i].Value);
                if (i < max)
                    builder.Append(",");
            }
            return builder.ToString();
        }
        return string.Empty;
    }
    protected void RadSave_Click(object sender, EventArgs e)
    {
        string id = Request.QueryString["id"];
        ltModel.Model.userTeam ut = ubl.GetModel(int.Parse(id));
        ut.sh = CheckBoxList1.Items[0].Selected ? "1" : "0";
        ut.tj = CheckBoxList1.Items[1].Selected ? "1" : "0";
        ut.add = CheckBoxList1.Items[2].Selected ? "1" : "0";
        ut.del = CheckBoxList1.Items[3].Selected ? "1" : "0";
        ut.funcqx = GetSelected(RadTreeView1);
        ut.lmqx = GetSelected(RadTreeView2);
        ubl.Update(ut);
        infoPanel.AddSuccessInfo("修改成功");
        DataCaches.SetUserTeam(int.Parse(id), ut);
        OperateLog.AddSuccessLog(CurrentUser.user_id, Request.UserHostAddress, "用户管理-设置权限>" + new ltModel.BLL.article_user().GetModel(int.Parse(id)).username);
        AddScriptM("setTimeout(parent.reloadData, 300);");
    }
}